<?php
class Edit extends CI_Controller {
	private $error = array();

	public function index() {
		if (!$this->customer->isLogged()) {
			$this->session->set_userdata('redirect', site_url('account/edit'));

			redirect(site_url('account/login'));
		}

		$this->lang->load('account/edit');
		
		$this->document->setTitle($this->lang->line('heading_title'));
		
		$this->load->model('account/Model_Customer');
		
		if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {
			$this->Model_Customer->editCustomer($this->input->post());
			
			$this->session->set_userdata('success', $this->lang->line('text_success'));

			redirect(site_url('account/account'));
		}

      	$data['breadcrumbs'] = array();

      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_home'),
			'href'      => site_url('common/home'),     	
        	'separator' => false
      	); 

      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_account'),
			'href'      => site_url('account/account'),        	
        	'separator' => $this->lang->line('text_separator')
      	);

      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_edit'),
			'href'      => site_url('account/edit'),       	
        	'separator' => $this->lang->line('text_separator')
      	);
		
		$data['heading_title'] = $this->lang->line('heading_title');
    	$data['text_gender_1'] = $this->lang->line('text_gender_1');
    	$data['text_gender_2'] = $this->lang->line('text_gender_2');
    	$data['text_gender_3'] = $this->lang->line('text_gender_3');

		$data['text_your_details'] = $this->lang->line('text_your_details');

		$data['entry_firstname'] = $this->lang->line('entry_firstname');
		$data['entry_lastname'] = $this->lang->line('entry_lastname');
    	$data['entry_firstname_en'] = $this->lang->line('entry_firstname_en');
    	$data['entry_lastname_en'] = $this->lang->line('entry_lastname_en');
    	$data['entry_gender'] = $this->lang->line('entry_gender');
    	$data['entry_email'] = $this->lang->line('entry_email');
    	$data['entry_mobile'] = $this->lang->line('entry_mobile');
		$data['entry_telephone'] = $this->lang->line('entry_telephone');
		$data['entry_fax'] = $this->lang->line('entry_fax');

		$data['button_continue'] = $this->lang->line('button_continue');
		$data['button_back'] = $this->lang->line('button_back');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->error['firstname'])) {
			$data['error_firstname'] = $this->error['firstname'];
		} else {
			$data['error_firstname'] = '';
		}

		if (isset($this->error['lastname'])) {
			$data['error_lastname'] = $this->error['lastname'];
		} else {
			$data['error_lastname'] = '';
		}
		
		if (isset($this->error['firstname_en'])) {
			$data['error_firstname_en'] = $this->error['firstname_en'];
		} else {
			$data['error_firstname_en'] = '';
		}	
		
		if (isset($this->error['lastname_en'])) {
			$data['error_lastname_en'] = $this->error['lastname_en'];
		} else {
			$data['error_lastname_en'] = '';
		}		
	
		if (isset($this->error['email'])) {
			$data['error_email'] = $this->error['email'];
		} else {
			$data['error_email'] = '';
		}
		
		if (isset($this->error['mobile'])) {
			$data['error_mobile'] = $this->error['mobile'];
		} else {
			$data['error_mobile'] = '';
		}
		
		if (isset($this->error['telephone'])) {
			$data['error_telephone'] = $this->error['telephone'];
		} else {
			$data['error_telephone'] = '';
		}	

		$data['action'] = site_url('account/edit');

		if ($this->input->server('REQUEST_METHOD') != 'POST') {
			$customer_info = $this->Model_Customer->getCustomer($this->customer->getId());
		}

		if ($this->input->post('firstname')) {
			$data['firstname'] = $this->input->post('firstname');
		} elseif (isset($customer_info)) {
			$data['firstname'] = $customer_info['firstname'];
		} else {
			$data['firstname'] = '';
		}

		if ($this->input->post('lastname')) {
			$data['lastname'] = $this->input->post('lastname');
		} elseif (isset($customer_info)) {
			$data['lastname'] = $customer_info['lastname'];
		} else {
			$data['lastname'] = '';
		}

		if ($this->input->post('firstname_en')) {
    		$data['firstname_en'] = $this->input->post('firstname_en');
		} elseif (isset($customer_info)) {
			$data['firstname_en'] = $customer_info['firstname_en'];
		} else {
			$data['firstname_en'] = '';
		}

		if ($this->input->post('lastname_en')) {
    		$data['lastname_en'] = $this->input->post('lastname_en');
		} elseif (isset($customer_info)) {
			$data['lastname_en'] = $customer_info['lastname_en'];
		} else {
			$data['lastname_en'] = '';
		}
		
		if ($this->input->post('gender')) {
    		$data['gender'] = $this->input->post('gender');
		} elseif (isset($customer_info)) {
			$data['gender'] = $customer_info['gender'];
		} else {
			$data['gender'] = 1;
		}
		
		if ($this->input->post('email')) {
    		$data['email'] = $this->input->post('email');
		} elseif (isset($customer_info)) {
			$data['email'] = $customer_info['email'];
		} else {
			$data['email'] = '';
		}
		
		if ($this->input->post('mobile')) {
    		$data['mobile'] = $this->input->post('mobile');
		} elseif (isset($customer_info)) {
			$data['mobile'] = $customer_info['mobile'];
		} else {
			$data['mobile'] = '';
		}

		if ($this->input->post('telephone')) {
			$data['telephone'] = $this->input->post('telephone');
		} elseif (isset($customer_info)) {
			$data['telephone'] = $customer_info['telephone'];
		} else {
			$data['telephone'] = '';
		}

		if ($this->input->post('fax')) {
			$data['fax'] = $this->input->post('fax');
		} elseif (isset($customer_info)) {
			$data['fax'] = $customer_info['fax'];
		} else {
			$data['fax'] = '';
		}

		$data['back'] = site_url('account/account');
		
		$data['header'] = helper_load_header();
		$data['column_left'] = helper_load_left();
		$data['column_right'] = helper_load_right();
		$data['content_top'] = helper_load_top();
		$data['content_bottom'] = helper_load_bottom();
		$data['footer'] = helper_load_footer();
		
		
		$this->load->view("account/edit", $data);
		
		
	}

	private function validate() {
    	if ((strlen(utf8_decode($this->input->post('firstname'))) < 1) || (strlen(utf8_decode($this->input->post('firstname'))) > 32)) {
      		$this->error['firstname'] = $this->lang->line('error_firstname');
    	}

    	if ((strlen(utf8_decode($this->input->post('lastname'))) < 1) || (strlen(utf8_decode($this->input->post('lastname'))) > 32)) {
      		$this->error['lastname'] = $this->lang->line('error_lastname');
    	}
    	if ((strlen(utf8_decode($this->input->post('firstname_en'))) < 1) || (strlen(utf8_decode($this->input->post('firstname_en'))) > 32)) {
      		$this->error['firstname_en'] = $this->lang->line('error_firstname');
    	}

    	if ((strlen(utf8_decode($this->input->post('lastname_en'))) < 1) || (strlen(utf8_decode($this->input->post('lastname_en'))) > 32)) {
      		$this->error['lastname_en'] = $this->lang->line('error_lastname');
    	}

		if ((strlen(utf8_decode($this->input->post('email'))) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->input->post('email'))) {
			$this->error['email'] = $this->lang->line('error_email');
		}
		
		if (($this->customer->getEmail() != $this->input->post('email')) && $this->Model_Customer->getTotalCustomersByEmail($this->input->post('email'))) {
			$this->error['warning'] = $this->lang->line('error_exists');
		}
		
    	if ((strlen(utf8_decode($this->input->post('mobile'))) < 8) || (strlen(utf8_decode($this->input->post('mobile'))) > 32)) {
      		$this->error['mobile'] = $this->lang->line('error_mobile');
    	}
		
    	if ((strlen(utf8_decode($this->input->post('telephone'))) < 8) || (strlen(utf8_decode($this->input->post('telephone'))) > 32)) {
      		$this->error['telephone'] = $this->lang->line('error_telephone');
    	}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}
?>